package com.example.demo.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 13795 on 2017/7/6.
 */
@Aspect
@Component
public class HttpAspact {
    protected Logger logger= LoggerFactory.getLogger(HttpAspact.class);
    @Pointcut("execution(public * com.example.demo.controller.*.*(..))")
    public void log(){

    }
    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        logger.info("方法执行前拦截");
        ServletRequestAttributes attribute =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attribute.getRequest();
        // url
        logger.info("url={}",request.getRequestURL());
        // method
        logger.info("method={}",request.getMethod());
        // id
        logger.info("id={}",request.getRemoteAddr());
        // 方法
        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        //方法名
        logger.info("class_methodName={}",joinPoint.getSignature().getName());
        // 参数
        logger.info("args={}",joinPoint.getArgs());
    }

    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object){
        logger.info("response={}",object);
    }
}
